﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Storage.Core.Extensions
{
    /// <summary>
    /// 
    /// </summary>
    public static class ModelStateExtension
    {
        /// <summary>
        /// 获取验证未通过的数据信息
        /// </summary>
        /// <param name="msDictionary"></param>
        /// <returns></returns>
        public static string getErrMsg(this ModelStateDictionary msDictionary)
        {
            if (msDictionary.IsValid || !msDictionary.Any()) return "";
            foreach (string key in msDictionary.Keys)
            {
                ModelState tempModelState = msDictionary[key];
                if (tempModelState.Errors.Any())
                {
                    var firstOrDefault = tempModelState.Errors.FirstOrDefault();
                    if (firstOrDefault != null)
                        return firstOrDefault.ErrorMessage;
                }
            }
            return "";
        }
    }
}
